<template>
  <div>
    <el-dialog title="员工导入" width="35%" :visible="dialogVisible" @close="close">
      <div class="main">
        <div class="content">
          <input type="file" accept=".xlsx, .xls" @change="fileChange" ref="inputRef" style="display:none">
          <div class="top">
            <i class="el-icon-upload"></i>
          </div>
          <div class="middle">
            <span>
              <el-button type="text" @click="downLoadTemplate">下载导入模板</el-button>
            </span>
          </div>
          <div class="bottom">
            <span>将文件拖到此处或</span>
            <span>
              <el-button type="text" @click="uploadFileTmp">点击上传</el-button>
            </span>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="danger" @click="$emit('update:dialogVisible',false)">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { templateExportRequest, uploadExcel } from "@/api/employee";
import FileSaver from 'file-saver'
export default {
  name: 'importExcel',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {

    }
  },
  created () {
  },
  computed: {
  },
  methods: {
    close () {
      this.$emit('update:dialogVisible', false)
    },
    //下载导入模板
    async downLoadTemplate () {
      const res = await templateExportRequest()
      console.log(res)
      //将二进制保存为一个文件
      FileSaver.saveAs(res, '上传员工的模板信息.xlsx') // 下载文件
    },
    //点击上传图片
    uploadFileTmp () {
      this.$refs.inputRef.click()
    },
    //批量-导入员工(上传excel)
    async fileChange (e) {
      console.log(e.target.files)
      const files = e.target.files
      if (files && files.length > 0) {
        //发送文件给后端,一般都要用 FormData
        const formData = new FormData()
        formData.append('file', files[0])
        try {
          await uploadExcel(formData)
          this.$emit('update:dialogVisible', false)
          this.$emit('getUserList')
        } catch (_) {

        } finally {
          this.$refs.inputRef.value = ''
        }
      }
    }
  }
}
</script>
<style lang='scss'  scoped>
.main {
  border: 2px dashed indianred;
  border-radius: 10px;
  width: 400px;
  margin: 0 auto;
  .content {
    padding: 20px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .top {
      .el-icon-upload::before {
        font-size: 50px;
        color: lightgrey;
      }
    }
    .middle {
      margin: 20px 0;
      span {
        color: lightcoral;
      }
    }
    .bottom {
      span {
        &:nth-child(1) {
          color: lightgrey;
        }
        &:nth-child(2) {
          color: lightcoral;
          margin-left: 10px;
        }
      }
    }
  }
}
</style>
